package com.zyd.shiro.business.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zyd.shiro.business.entity.Stock;
import com.zyd.shiro.business.enums.ErrorEnum;
import com.zyd.shiro.business.service.PakStockService;
import com.zyd.shiro.framework.object.BaseConditionVO;
import com.zyd.shiro.persistence.beans.PakOrderLine;
import com.zyd.shiro.persistence.beans.PakOut;
import com.zyd.shiro.persistence.beans.PakOutLine;
import com.zyd.shiro.persistence.beans.PakStock;
import com.zyd.shiro.persistence.mapper.PakOutMapper;
import com.zyd.shiro.persistence.mapper.PakStockMapper;
import lombok.extern.slf4j.Slf4j;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;


@Slf4j
@Service
public class PakStockServiceImpl implements PakStockService {

    @Autowired
    private PakStockMapper pakStockMapper;

    @Autowired
    private PakOutMapper pakOutMapper;

    @Override
    public Stock insert(Stock stock) {
        Assert.notNull(stock, "对象不可为空！");
        stock.getPakStock().setUpdateTime(new Date());
        stock.getPakStock().setCreateTime(new Date());
        pakStockMapper.insertSelective(stock.getPakStock());
        return stock;
    }

    @Override
    public void insertList(List<Stock> stocks) {
            for(Stock stock:stocks){
               stock.getPakStock().setUpdateTime(new Date());
               stock.getPakStock().setCreateTime(new Date());
               pakStockMapper.insertSelective(stock.getPakStock());
           }
    }


    /**
     * 根据主键字段进行删除，方法参数必须包含完整的主键属性
     *
     * @param primaryKey
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean removeByPrimaryKey(Long primaryKey) {
        return pakStockMapper.deleteByPrimaryKey(primaryKey) > 0;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean update(Stock stock) {
        Assert.notNull(stock, "对象不可为空！");
        stock.setUpdateTime(new Date());
        return pakStockMapper.updateByPrimaryKey(stock.getPakStock()) > 0;
    }

    @Override
    public boolean updateSelective(Stock stock) {
        Assert.notNull(stock, "对象不可为空！");
        stock.setUpdateTime(new Date());
        return pakStockMapper.updateByPrimaryKeySelective(stock.getPakStock()) > 0;
    }

    /**
     * 根据主键字段进行查询，方法参数必须包含完整的主键属性，查询条件使用等号
     *
     * @param primaryKey
     * @return
     */

    @Override
    public Stock getByPrimaryKey(Long primaryKey) {
        PakStock pakStock = pakStockMapper.selectByPrimaryKey(primaryKey);
        return null == pakStock ? null : new Stock(pakStock);
    }


    /**
     * 根据实体中的属性进行查询，只能有一个返回值，有多个结果时抛出异常，查询条件使用等号
     *
     * @param
     * @return
     */
    @Override
    public Stock getOneByEntity(Stock test) {
        Assert.notNull(test, "对象不可为空！");
        PakStock pakStock = pakStockMapper.selectOne(test.getPakStock());
        return null == pakStock ? null : new Stock(pakStock);
    }

    @Override
    public List<Stock> listAll() {
        List<PakStock> pakStocks = pakStockMapper.selectAll();
        if (CollectionUtils.isEmpty(pakStocks)) {
            return null;
        }
        List<Stock> stocks = new ArrayList<>();
        for (PakStock pakStock : pakStocks) {
            stocks.add(new Stock(pakStock));
        }
        return stocks;
    }

    @Override
    public List<Stock> listByEntity(Stock entity) {
        return null;
    }


    /**
     * 分页查询
     *
     * @param vo
     * @return
     */
    @Override
    public PageInfo<Stock> findPageBreakByCondition(BaseConditionVO vo) {
        PageHelper.startPage(vo.getPageNumber(), vo.getPageSize());
        List<PakStock> pakStocks = pakStockMapper.findPageBreakByCondition(vo);
        if (CollectionUtils.isEmpty(pakStocks)) {
            return null;
        }
        List<Stock> stocks = new ArrayList<>();
        for (PakStock pakStock : pakStocks) {
            stocks.add(new Stock(pakStock));
        }
        PageInfo bean = new PageInfo<PakStock>(pakStocks);
        bean.setList(stocks);
        return bean;
    }
    @Override
    public void increase(PakStock pakStock) {
        pakStockMapper.insert(pakStock);
    }



    @Override
    public Integer outWare(PakStock pakStock) {
        PakStock orStock = pakStockMapper.findOnlyByMaterielAndDepot(pakStock.getMateriel(),pakStock.getDepot());
        if(orStock == null){
            return ErrorEnum.OUT_NULL.getCode();
        }
        if(orStock.getNumber()<pakStock.getNumber()){
            return ErrorEnum.OUT_NUMBER.getCode();
        }
        orStock.setNumber(orStock.getNumber()-pakStock.getNumber());
        orStock.setTotal(orStock.getTotal()-(orStock.getAmt()* orStock.getNumber()));
        pakStockMapper.updateByPrimaryKeySelective(orStock);
        return ErrorEnum.SUCCESS.getCode();
    }

    @Override
    public Integer outWare(PakOutLine outLine) {
        PakOut pakOut = pakOutMapper.selectByPrimaryKey(outLine.getOut());
        PakStock pakStock = new PakStock();
        pakStock.setMateriel(outLine.getMateriel());
        pakStock.setDepot(pakOut.getDepot());
        pakStock.setNumber(outLine.getNumber());
        return outWare(pakStock);
    }

    @Override
    public void enterWare(PakStock pakStock) {
        PakStock orStock = pakStockMapper.findOnlyByMaterielAndDepot(pakStock.getMateriel(),pakStock.getDepot());
        if(orStock != null){
            orStock.setAmt((orStock.getAmt()+pakStock.getAmt())/2);
            orStock.setNumber(orStock.getNumber()+pakStock.getNumber());
            orStock.setTotal(orStock.getTotal()+pakStock.getTotal());
            pakStockMapper.updateByPrimaryKeySelective(orStock);
        }else {
            pakStockMapper.insert(pakStock);
        }

    }

    @Override
    public void enterWare(PakOrderLine orderLine, Long depotId) {
        PakStock stock = new PakStock();
        stock.setMateriel(orderLine.getMateriel());
        stock.setAmt(orderLine.getAmt());
        stock.setNumber(orderLine.getNumber());
        stock.setTotal(orderLine.getTotal());
        stock.setDepot(depotId);

        enterWare(stock);
    }


}
